package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 *
 * 如果写了 redirect, forward: 则视图解析器，不添加前缀和后缀
 *
 * 在处理器 括号中,可以帮助我们创建
 *  自动注入：HttpServletRequest， HttpServletResponse， HttpSession,
 *  自动创建：Model， User  (new出来的)
 */
@Controller
@SessionAttributes(names = {"name","pwd"})
public class Test {

    /**
     * Model：在请求域中添加数据  model.addAttribute() , 返回值就是路径
     * ModelMap:在请求域中添加数据 model.put() 返回值就是路径
     * ModelAndView: 在请求域添加数据，设置跳转路径  mv.addObject()， 返回值必须是ModelAndView
     */
    @RequestMapping("/test01")
//    public String test01(Model model){
//        model.addAttribute("name","张三");
//    public String test01(ModelMap modelMap){
//        modelMap.put("name","李四");
    public ModelAndView test01(ModelAndView modelAndView){

        modelAndView.addObject("name","李四2");
        modelAndView.setViewName("forward:/jsp/index.jsp");
        return modelAndView;
//        return "forward:/jsp/index.jsp";
    }

//    @RequestMapping("/test02")
//    public String test02(){
//        return "redirect:/jsp/index.jsp";
//    }

//    @SessionAttributes(names = {"id", "username"})
//    指定在session中存储的对象的名称
//    @RequestMapping("/test02")
//    public String test02(Model model){
//        model.addAttribute("name","zzz123");
//        model.addAttribute("pwd",123456);
//        return "redirect:/jsp/index.jsp";
//    }

    @RequestMapping("/test02")
    public String test02(HttpSession session){
        session.setAttribute("name","zzz123");
        session.setAttribute("pwd",123456);
        return "redirect:/jsp/index.jsp";
    }
}
